import {
  addCar,
  delCar
} from '../../../utils/networkRequest.js'
const app = getApp()
Page({
  /**
   * 页面的初始数据
   * keyboard1:首页键盘,显示省的简称
   * keyboard2:第二页键盘，显示数字和大写字母
   */

  data: {
    isKeyboard: false, //是否显示键盘
    specialBtn: false,
    tapNum: false, //数字键盘是否可以点击
    parkingData: false, //是否展示剩余车位按钮
    isFocus: false, //输入框聚焦
    isFocusP: false, //手机输入框聚焦
    isFocusV: false, //验证码输入框聚焦
    isFocusN: false, //姓名输入框聚焦
    flag: false, //防止多次点击的阀门 
    keyboardNumber: '1234567890',
    keyboardAlph: 'QWERTYU OPASDFGHJKLZXCVBNM巛',
    keyboard1: '京津沪冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤川青藏琼宁渝',
    keyboard2: '',
    keyboard2For: ['完成'],
    keyboardValue: '',
    textArr: [],
    animationData: {},
    textValue: '', //车牌号
    inputPhoneValue: "", //手机号
    inputNameValue: "", //姓名 

    carInfo: null, //车辆信息
    isEdit: false, //是否是修改车辆信息
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (options.isEdit) {
      let carInfo = JSON.parse(options.item);
      debugger;
      this.setData({
        isEdit: options.isEdit,
        carInfo: carInfo,
        textValue: carInfo.auto_number,
        inputPhoneValue: carInfo.phone,
        inputNameValue: carInfo.name,
        isFocus: true,
      })
    }
  },
  /**
   * 输入框显示键盘状态
   */
  showKeyboard: function() {
    var self = this;
    self.setData({
      isFocus: true,
      isKeyboard: true,
    })
  },



  /**
   * 点击页面隐藏键盘事件
   */
  hideKeyboard: function() {
    var self = this;
    if (self.data.isKeyboard) {
      //说明键盘是显示的，再次点击要隐藏键盘
      if (self.data.textValue) {
        self.setData({
          isKeyboard: false
        })
      } else {
        self.setData({
          isKeyboard: false,
          isFocus: false
        })
      }

    }
  },
  /**
   * 输入框聚焦触发，显示键盘
   */
  bindFocus: function() {
    var self = this;
    if (self.data.isKeyboard) {
      //说明键盘是显示的，再次点击要隐藏键盘
      self.setData({
        isKeyboard: false,
        isFocus: true,
      })
    } else {
      //说明键盘是隐藏的，再次点击显示键盘
      self.setData({
        isFocus: true,
        isKeyboard: true,
      })
    }
  },
  /**
   * 键盘事件
   */
  tapKeyboard: function(e) {
    var self = this;
    //获取键盘点击的内容，并将内容赋值到textarea框中
    var tapIndex = e.target.dataset.index;
    var tapVal = e.target.dataset.val;
    var keyboardValue;
    var specialBtn;
    var tapNum;
    if (tapVal == "巛") {
      //说明是删除 
      self.data.textArr.pop();
      if (self.data.textArr.length == 0) {
        //说明没有数据了，返回到省份选择键盘
        this.specialBtn = false;
        this.tapNum = false;
        this.keyboardValue = self.data.keyboard1;
      } else if (self.data.textArr.length == 1) {
        //只能输入字母
        this.tapNum = false;
        this.specialBtn = true;
        this.keyboardValue = self.data.keyboard2;
      } else {
        this.specialBtn = true;
        this.tapNum = true;
        this.keyboardValue = self.data.keyboard2;
      }
      self.data.textValue = self.data.textArr.join("");
      self.setData({
        textValue: self.data.textValue,
        keyboardValue: this.keyboardValue,
        specialBtn: this.specialBtn,
        tapNum: this.tapNum,
      })
      return false
    }
    if (self.data.textArr.length >= 8) {
      return false;
    }
    self.data.textArr.push(tapVal);
    self.data.textValue = self.data.textArr.join("");
    self.setData({
      textValue: self.data.textValue,
      keyboardValue: self.data.keyboard2,
      specialBtn: true,
    })
    if (self.data.textArr.length > 1) {
      //展示数字键盘
      self.setData({
        tapNum: true
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    var self = this;
    //将keyboard1和keyboard2中的所有字符串拆分成一个一个字组成的数组
    self.data.keyboard1 = self.data.keyboard1.split('')
    self.data.keyboard2 = self.data.keyboard2.split('')
    self.setData({
      keyboardValue: self.data.keyboard1
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */


  onShow: function() {
    var self = this;
    self.setData({
      flag: false
    })
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: 'ease',
    })
    setInterval(function() {
      if (self.data.opacity == 1) {
        animation.opacity(0).step()
        this.setData({
          animationData: animation.export(),
          opacity: 0,
        })
      } else {
        animation.opacity(1).step()
        this.setData({
          animationData: animation.export(),
          opacity: 1,
        })
      }
    }.bind(this), 600)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    wx.stopPullDownRefresh()
  },
  /**
   * 姓名接收
   */
  bindNameKeyInput: function(e) {
    this.setData({
      inputNameValue: e.detail.detail.value
    })
  },
  /**
   * 手机号接收
   */
  bindPhoneKeyInput: function(e) {
    this.setData({
      inputPhoneValue: e.detail.detail.value
    })
  },
  // 前往添加车辆
  goAddCar: function(e) {
    debugger;
    let {
      carInfo
    } = this.data
    let id = "";
    if (carInfo) {
      id = carInfo.id
    }
    let {
      textValue,
      inputPhoneValue,
      inputNameValue
    } = this.data;
    addCar(id, textValue, inputNameValue, inputPhoneValue, (r) => {
      if (r.code == 0) {
        wx.showToast({
          icon: "none",
          title: id ? "修改成功" : "添加成功",
        })
        wx.navigateBack();
      }
    })
  },
  //删除车辆
  delCar: function() {
    let self = this;

    wx.showModal({
      title: '提示',
      content: '确定要删除该车主信息吗？',
      success: function (res) {
        if (res.confirm) {

          delCar(self.data.carInfo.id, (r) => {
            if (r.code == 0) {
              wx.showToast({
                icon: 'none',
                title: '删除成功',
              })
              wx.navigateBack();

            }
          })
        } else if (res.cancel) {
          //点击取消
        }
      }
    })
   
  }

})